import 'package:flutter/services.dart';
import 'package:flutterproject/mvvm/base/base_controller.dart';
import 'package:flutterproject/page/mine/data/mine_index_bean.dart';
import 'package:flutterproject/page/mine/data/my_assets_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:get/get.dart';

class MineController extends BaseController {
  HttpClient client = Get.find<HttpClient>();

  MineIndexBean? mineIndexBean;
  MyAssetsBean? myAssetsBean = MyAssetsBean();

  @override
  void onReady() {
    super.onReady();
    getMineIndexData();
    getMyAssetsData();
  }

  Future getMineIndexData() async {
    try {
      DialogUtil.showLoading();
      mineIndexBean = await client.get<MineIndexBean>(AddressManager.mineIndex);
      update();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  Future<void> copyInviteCode() async {
    await Clipboard.setData(
        ClipboardData(text: mineIndexBean?.shareCode ?? ""));
    ToastUtil.showSuccess("复制成功!");
  }

  Future getMyAssetsData() async {
    try {
      myAssetsBean = await client.get<MyAssetsBean>(AddressManager.myAssets);
      update();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    }
  }

  String getVIPLevel (){
    switch (mineIndexBean?.level ?? -1) {
      case 0:
        return "普通会员";
      case 1:
        return "VIP";
      case 2:
        return "代理商";
      case 3:
        return "区代理";
      case 4:
        return "市代理";
      case 5:
        return "大区总监";
      case 6:
        return "营销总监";
      case 7:
        return "营销总裁";
    }
    return "普通会员";
  }
}
